import numpy as np
import matplotlib.pyplot as plt
from chaco.api import *
from traits.api import *
from traitsui.api import *
from enable.api import ComponentEditor


def atmospheric_refraction(height):
    n_earth = 32
    delta_n=-7.32*np.exp(0.005577*n_earth)
    c_e=np.log(n_earth/(n_earth+delta_n))
    n_h=n_earth*np.exp(-c_e*height)
    return n_h

def radar_target_altitude_angle(theta_r,h_r,h_t):
    n_r = atmospheric_refraction(h_r)
    n_t=atmospheric_refraction(h_t)
    theta_r=theta_r*np.pi/180
    theta_t=np.arccos(n_r*(6400+h_r)*np.cos(theta_r)/n_t/h_t)
    theta_t = theta_t / np.pi * 180
    return theta_t


def radar_target_integral(theta_rt,h_r,h_t):
    # xx=np.linspace(h_t,h_r,100)
    # delta_xx=xx[1]-xx[0]
    # n_r = atmospheric_refraction(h_r)
    # n = atmospheric_refraction(xx)
    # theta_rt=theta_rt*np.pi/180
    #
    # n=32#遇到0除数
    #
    # yy=1/np.sqrt(1-np.power(n_r*(6400+h_r)*np.cos(theta_rt)/n/(6400+xx),2))
    # integral_result=np.sum(yy*delta_xx)
    return 1


class radar_target(HasTraits):
    radar_height = Range(low=0.1, high=60.0, value=1)
    target_height = Range(low=0.1, high=60.0, value=1)
    data = Property(Array, observe=['radar_height', 'target_height'])
    plot = Instance(Plot)

    def _plot_default(self):
        theta_t = np.arange(0, 10)
        radar_angle = radar_target_altitude_angle(theta_t, self.radar_height, self.target_height)
        plot = Plot(ArrayPlotData(theta_t=theta_t, radar_angle=radar_angle))
        plot.title = 'target-radar-angle-curve'
        plot.x_axis.title = "target height"
        plot.y_axis.title = "target angle"
        plot.plot(("theta_t", "radar_angle"), name='radar_target_angle', color=palette11[0])
        return plot

    def _get_data(self):
        data={}
        theta_t = np.arange(0, 10)
        radar_angle = radar_target_altitude_angle(theta_t, self.radar_height, self.target_height)
        data['x'] = theta_t
        data['y'] = radar_angle
        return data

    @observe("data")
    def _update_plot(self, event):
        self.plot.data.update_data(
            theta_t=self.data['x'],
            radar_angle=self.data['y'],
        )

    traits_view = View(
        Group(
            UItem(
                "plot",
                editor=ComponentEditor(),
                resizable=True
            )
        ),
        Item('radar_height', label='雷达高度', editor_args={'mode': 'slider'}),
        Item('target_height', label='目标高度', editor_args={'mode': 'slider'}),
    )



class radar_target_distance(HasTraits):
    radar_height = Range(low=0.1, high=60.0, value=1)
    target_height = Range(low=0.1, high=60.0, value=1)
    data = Property(Array, observe=['radar_height', 'target_height'])
    plot = Instance(Plot)

    def _plot_default(self):
        theta_t = np.arange(0, 10)
        radar_angle = radar_target_altitude_angle(theta_t, self.radar_height, self.target_height)
        plot = Plot(ArrayPlotData(theta_t=theta_t, radar_angle=radar_angle))
        plot.title = 'target-radar-angle-curve'
        plot.x_axis.title = "target height"
        plot.y_axis.title = "target angle"
        plot.plot(("theta_t", "radar_angle"), name='radar_target_angle', color=palette11[0])
        return plot

    def _get_data(self):
        data = {}
        theta_t = np.arange(0, 10)
        radar_angle = radar_target_altitude_angle(theta_t, self.radar_height, self.target_height)
        data['x'] = theta_t
        data['y'] = radar_angle
        return data

    @observe("data")
    def _update_plot(self, event):
        self.plot.data.update_data(
            theta_t=self.data['x'],
            radar_angle=self.data['y'],
        )

    traits_view = View(
        Group(
            UItem(
                "plot",
                editor=ComponentEditor(),
                resizable=True
            )
        ),
        Item('radar_height', label='雷达高度', editor_args={'mode': 'slider'}),
        Item('target_height', label='目标高度', editor_args={'mode': 'slider'}),
    )



class radar_target_geoangle(HasTraits):
    radar_height = Range(low=0.1, high=60.0, value=1)
    target_height = Range(low=0.1, high=60.0, value=1)
    data = Property(Array, observe=['radar_height', 'target_height'])
    plot = Instance(Plot)

    def _plot_default(self):
        theta_t = np.arange(0, 10)
        radar_angle = radar_target_altitude_angle(theta_t, self.radar_height, self.target_height)
        plot = Plot(ArrayPlotData(theta_t=theta_t, radar_angle=radar_angle))
        plot.title = 'target-radar-angle-curve'
        plot.x_axis.title = "target height"
        plot.y_axis.title = "target angle"
        plot.plot(("theta_t", "radar_angle"), name='radar_target_angle', color=palette11[0])
        return plot

    def _get_data(self):
        data = {}
        theta_t = np.arange(0, 10)
        radar_angle = radar_target_altitude_angle(theta_t, self.radar_height, self.target_height)
        data['x'] = theta_t
        data['y'] = radar_angle
        return data

    @observe("data")
    def _update_plot(self, event):
        self.plot.data.update_data(
            theta_t=self.data['x'],
            radar_angle=self.data['y'],
        )

    traits_view = View(
        Group(
            UItem(
                "plot",
                editor=ComponentEditor(),
                resizable=True
            )
        ),
        Item('radar_height', label='雷达高度', editor_args={'mode': 'slider'}),
        Item('target_height', label='目标高度', editor_args={'mode': 'slider'}),
    )


class Form(HasTraits):
    radar_target = Instance(radar_target, ())
    radar_target_distance = Instance(radar_target_distance, ())
    radar_target_geoangle=Instance(radar_target_geoangle,())
    traits_view = View(
        Tabbed(
            UItem('radar_target', style='custom', label='方位角'),
            UItem('radar_target_distance', style='custom', label='传播距离'),
            UItem('radar_target_geoangle', style='custom', label='地心角'),
        ),
        title='变化曲线',
        width=1200,
        height=800,
        resizable=True,
    )

if __name__ == '__main__':
    Form().configure_traits()